package org.zxp.datastructure.map;

import java.util.WeakHashMap;

/**
 * @program: effectiveJava
 * @description: WeakHashMap用法，当key没有引用时，自动回收，注意key value不能是同一个
 * @author: X-Pacific zhang
 * @create: 2019-05-21 15:59
 **/
public class WeakHashMapTest {
    public static void main(String[] args){
        WeakHashMap weakHashMap = new WeakHashMap();
        Object o1 = new Object();
        weakHashMap.put(o1,"1");
        Object o2 = new Object();
        weakHashMap.put(o2,"2");
        weakHashMap.forEach((k,v) -> System.out.println("k:"+k+" v:"+v));
        System.out.println();
        o2 = null;
        System.gc();
        weakHashMap.forEach((k,v) -> System.out.println("k:"+k+" v:"+v));
    }


}
